# -*- coding:utf-8 -*-
import requests
import json
import csv

class TestMaterial():

    def __init__(self):
        print("start")



    def materdetect(self):
        # 初始化
        material_flag_init = 0
        material_error_init = 0
        material_fail_init = 0
        # 初始化登录接口
        login_url = 'https://api.icarbonx.com/oauth2/token?grant_type=password&sms_verify=true'
        login_header = {"Authorization": "Basic Y29tLm1ldW0uY29hY2guaXBob25lOjdmYTYyZmFmYzgxZTgwMzY="}
        login_data = {
            "username": "15012345678",
            "password": "888888",
            "appName": "health-buddy",
            "grant_type": "password",
            "sms_verify": "true"
        }
        # 登录请求接口
        r_login = requests.post(url=login_url, data=login_data, headers=login_header)

        # 读取csv文件
        with open('E:\\test\\HB\\material.csv', 'r') as csvFile:
            reader = csv.reader(csvFile)
            print(type(reader))
            next(reader)
            for row in reader:
                print(row)
                print(type(row))
                print(row[0])
                print(type(row[0]))

                # 食物qa问答接口的请求参数msg
                material_msg = {"msg": row[0]}
                print(material_msg)

                # 获取登录的响应报文
                print(r_login.text)
                login_response = json.loads(r_login.text)
                # 保存登录的token信息
                access_token = login_response['access_token']
                material_headers = {"Authorization": "Bearer " + access_token}
                # qa问答的url
                material_url = 'https://api.icarbonx.com/nlp/api/v1.0/food_detect'
                # qa问答模型接口请求
                r_material = requests.post(url=material_url, data=material_msg, headers=material_headers)
                # 获取响应报文
                print(r_material.text)
                # 转换响应结果为dict格式
                material_response = json.loads(r_material.text)
                print(material_response)
                print(type(material_response))

                # 判断响应结果是否为空,不为空，则获取material和name的名称
                if material_response:
                    print(material_response[0])
                    material_response_all = material_response[0]
                    material_response_all_one = material_response_all['properties']
                    # 获取cal_name的值
                    material_response_cal = material_response_all_one['cal_name']
                    material_response_name = material_response_all_one['name']
                    print("material：" + material_response_cal)

                    if material_response_cal == material_response_name :
                        # 如果calname和name一致，则测试通过，写入material_py_success文件

                        material_flag_init = material_flag_init + 1
                        print("material匹配成功%d" %material_flag_init)
                    else:
                        fail_data = ['calname','name']
                        material_fail_init = material_fail_init + 1
                        print("material匹配错误%d" %material_fail_init)
                        with open('E:\\test\\HB\\material_py_fail.csv','a+',encoding='utf-8-sig') as ff:
                            fail_data[0] = material_response_cal
                            fail_data[1] = material_response_name
                            ff.write(','.join(fail_data))
                            ff.write('\n')
                            ff.close()

                else:
                    material_error_init = material_error_init + 1
                    print("material匹配失败%d" % material_error_init)

                    # 将失败的食物name存在csv文件
                    error_data = ['name']
                    with open('E:\\test\\HB\\material_py_error.csv','a+',encoding = 'utf-8-sig')as ef:
                        error_data[0] = row[0]
                        ef.write(','.join(error_data))
                        ef.write('\n')
                        ef.close()

                    # 如何qa的分值大于闲聊，则返回qa的answer
                data_row = ['material_response_cal', 'material_response_name']
                with open('E:\\test\\HB\\material_py.csv','a+',encoding='utf-8-sig') as f:
                    # csv_write = csv.writer(f)
                    data_row[0] = material_response_cal
                    data_row[1] = material_response_name



                        # data_row = list(data_row)
                        # print(data_row)
                    f.write(','.join(data_row))
                    f.write("\n")
                    # f.write("\\n")
                    print("down")
                    f.close()

                    # 打印食物匹配结果
                    print("material匹配成功%d" % material_flag_init)
                    print("material匹配错误%d" % material_fail_init)
                    print("material匹配失败%d" % material_error_init)


        csvFile.close()




if __name__ == '__main__':
    fd = TestMaterial()
    fd.materdetect()